package com.linewell.service.impl;

import com.google.gson.Gson;
import com.linewell.dao.ArticleMapper;
import com.linewell.dao.LetterMapper;
import com.linewell.dao.UserInfoMapper;
import com.linewell.entity.Letter;
import com.linewell.entity.UserInfo;
import com.linewell.service.ILetterService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class LetterService implements ILetterService {

    @Autowired
    LetterMapper letterDao;
    @Autowired
    UserInfoMapper userInfoDao;
    @Autowired
    ArticleMapper articleDao;

   /**
    * @descripition  插入留言信息
    * @author hyuhuang@linewell.com
    * @date 2018/2/27 12:35
    * @param letterStr, request
    * @return int
    */
    public int insertLetter(String letterStr,HttpServletRequest request) {
        String letterId = UUID.randomUUID().toString().replaceAll("-", "");
        Gson gson=new Gson();
        Letter letter=gson.fromJson(letterStr,Letter.class);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("LoginUser");
        letter.setSendTime(sdf.format(new Date()));
        letter.setIsRead(0);
        letter.setWriterUser(userInfo);
        letter.setLetterId(letterId);
        return letterDao.insertLetter(letter);
    }

    public Msg selectLetter(Long userId) {
        List<Letter> letterList = letterDao.selectLetter(userId);
        int count = letterList.size();
        return Msg.success().add("count",count).add("letterList",letterList);
    }

    public Msg updateLetterIsRead(String letterId) {
        if (letterDao.updateLetterIsRead(letterId)){
            return Msg.success();
        } else {
            return Msg.fail();
        }

    }
    /**
     * @Description 通过父id查找其下的回复信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/18
     * @param letterId
     * @return com.linewell.utils.Msg
     */
    public Msg getLetterById(String letterId) {
        List<Letter> letterList = letterDao.getLetterById(letterId);
        if (letterList.size()>0){
            return Msg.success().add("letterList",letterList);
        } else{
            return Msg.fail();
        }
    }

  /*  public List<Letter> getChildren(Letter letters){
        if (letters.getChildren() == null){
            return listList;
        } else {
            List<Letter> lt = letterDao.getLetterById(letters.getChildren());
            listList.add(lt.get(0));
            getChildren(lt.get(0));
        }
        return listList;
    }*/


    public Msg insertLetterReply(Long userId, String content,String letterParentId,Long writer) {
        Letter letter = new Letter();
        String letterId = UUID.randomUUID().toString().replaceAll("-", "");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        letter.setIsRead(0);    //未读
        letter.setSendTime(sdf.format(new Date()));   //设置时间
        letter.setContent(content);   //设置内容
        letter.setLetterId(letterId); //设置id
        letter.setWriterUser(userInfoDao.getUserById(userId));   //设置回复人
        letter.setReaderUser(userInfoDao.getUserById(writer));
        letter.setParentId(letterParentId);
        if (letterDao.insertLetter(letter)!=0){
            return Msg.success().add("letter",letter);
        } else {
            return Msg.fail();
        }
    }

    public String getUserByletterId(String letterId) {
        return letterDao.getUserByletterId(letterId);
    }

    public Msg opinion(String content, Long articleId) {
        Letter letter = new Letter();
        String letterId = UUID.randomUUID().toString().replaceAll("-", "");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        letter.setIsRead(3);    //系统消息未读
        letter.setSendTime(sdf.format(new Date()));   //设置时间
        letter.setContent(content);   //设置内容
        letter.setLetterId(letterId); //设置id
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(1L);
        letter.setWriterUser(userInfo);   //设置回复人
        UserInfo article = new UserInfo();
        article.setUserId(articleId);
        letter.setReaderUser(article);
        if (letterDao.opinion(letter)){
            articleDao.updataArticleCheck(articleId,3);
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg selectSystemLetter(Long userId) {
        List<Letter> letterList = letterDao.selectSystemLetter(userId);
        int count = letterList.size();
        return Msg.success().add("count",count).add("letterList",letterList);
    }

    public Msg getArticleByletterId(String letterId) {
        return Msg.success().add("article",letterDao.getArticleByLetterId(letterId));
    }

    public Msg selectLetters(Long userId) {
        List<Letter> letterList = letterDao.selectLetter(userId);
        List<Letter> letters = letterDao.selectSystemLetter(userId);
        for (Letter let:letters) {
            letterList.add(let);
        }
        int count = letterList.size();
        return Msg.success().add("count",count).add("letterList",letterList);
    }
}
